<?php 

	require_once("src/model/ValidateLoginInfo.php"); 
	
	
class LoginForm
{
	private $loginValidation;

	private static $isLogin = "login";
	private static $isLogout = "logout";

	private static $postUsername = "Loginform::username";
	private static $postPassword = "Loginform::userPassword";

	private static $rememberMe = "Loginform::rememberMe";
	private static $rememberMeValue = "RememberMe";
	
	private static $usernameCookie = "Loginform::usernameCookie";
	private static $passwordCookie = "Loginform::PasswordCookie";


	public function __construct()
	{
		$this->loginValidation = new LoginValidation();
	}

	/**
	 * @param $errorMessage string
	 * @param $lastUsername string
	 * 
	 * @return string, loginForm.
	 */
	public function GetForm($errorMessage, $lastUsername)
	{		
		return 
		"<div id='loginForm'>
			<h2>Ej inloggad </h2>
			  
			<form action='?login'  method='post' id='form1'  >
				<fieldset>
					<legend> Skriv in ditt användarnamn och lösenord! </legend>
					<p>$errorMessage</p>
					<label for='usernameInput'> Användarnamn:  </label>
					<input id='usernameInput' type='text' name='" . self::$postUsername ."' 
				  		value='$lastUsername' /> 
					
					<label for='userPassInput'> Password: </label>
					<input id='userPassInput' type='text' name='" . self::$postPassword . "' value='' /> 
					<label>Håll mig inloggad:
						<input type='checkbox' name='". self::$rememberMe ."' value='". self::$rememberMeValue ."'/>
					</label>	
					<input type='submit' name='Submit' value='Logga in' />
				</fieldset>
			</form>
		 </div>";
	}


	public function isLogedOut()
	{
		if(isset($_GET[self::$isLogout]))
		{
			return true;
		}
		return false;
	}

	public function isLogin()
	{
		if(isset($_GET[self::$isLogin]))
		{
			return true;
		}
		return false;
	}

	/**
	 * @return string, Session - username
	 */
	public function GetUsername()
	{
		if(!empty($_POST[self::$postUsername]))
		{
			return $_POST[self::$postUsername];
		}
		else {
			return"";
		}
	}
	
	/**
	 * @return string, Session - userpassword
	 */
	public function GetUserPassword()
	{
		if(!empty($_POST[self::$postPassword]))
		{
			return $_POST[self::$postPassword];
		}
		else 
		{
			return "";
		}
	}
	
	/**
	 * @Return boolean. True if the user wants us to 
	 * remember them.
	 */
	public function RememberMe()
	{	
		if(isset($_POST[self::$rememberMe]))
		{
			if($_POST[self::$rememberMe] == self::$rememberMeValue)
			{
				if(!$this->AreLoginCookiesSet())
				{ 	
					$endtime = time()+30; 
					$this->loginValidation->saveCookieEndTime($endtime);

					setcookie(self::$usernameCookie, $this->GetUsername(), $endtime);
					setcookie(self::$passwordCookie, md5($this->GetUserPassword()), $endtime);
					
				}
				return true;
			}	
		}
		return false;
	}
	
	/**
	 * @return boolean, true if login Cookies are set.
	 */
	public function AreLoginCookiesSet()
	{
		if(isset($_COOKIE[self::$usernameCookie])&& $_COOKIE[self::$passwordCookie])
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * @return boolean
	 */
	public function CookiesExistsAndAreOK()
	{
		$validateLoginInfo = new LoginValidation();

			if($this->CheckValidCookieTime())  // förutsätter att kakorna är satta..tiden är OK, så kollar vi validering.
			{
				if($validateLoginInfo->CookieLoginValidation( 
			    $_COOKIE[self::$usernameCookie],$_COOKIE[self::$passwordCookie]))
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;	
			}
	}	
	
	public function doWeHaveAnyCookies()
	{
		if(isset($_COOKIE[self::$usernameCookie]) || isset($_COOKIE[self::$passwordCookie]))
		{
			return TRUE;
		}
		return false;
	}
	/**
	 * @return boolean
	 */
	public function OnlyOneCookiesExists()
	{
		if(isset($_COOKIE[self::$usernameCookie]) && !isset($_COOKIE[self::$passwordCookie]) ||
		  !isset($_COOKIE[self::$usernameCookie]) && isset($_COOKIE[self::$passwordCookie]))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	//TODOO STRÄNBEROENDEN.
	
	/**
	 * @return boolean, false if cookie end time has been messed around with.
	 */
	public function CheckValidCookieTime()
	{
		$endTime = $this->loginValidation->getCookieEndTime();   //TODOO KALLA PÅ MODEL KLASS FÖR DET HÄR.
	
		if(time() > $endTime)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	

	public function DestroyLoginCookies()
	{	
		setcookie(self::$usernameCookie, "", time()-3600);
		setcookie(self::$passwordCookie,"" ,time()-3600);
	}

	/**
	 * @Return string, username
	 */
	public function GetCookieUsername()
	{
		 
		if(isset($_COOKIE[self::$usernameCookie]))
		{
			return $_COOKIE[self::$usernameCookie];
		}
		else
		{
			return "";
		} 
	}
	 
	 /**
	 * @Return string, userpassword
	 */
	public function GetCookieUserPassword()
	{
		
		if(isset($_COOKIE[self::$passwordCookie]))
		{
			 return $_COOKIE[self::$passwordCookie];
		}
		else
		{
			return "";
		} 
	}
	
	
}

